import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution446 {

    public static void main(String[] args) {
        int[] arr = { 7,7,7,7, 7} ;
        System.out.println(numberOfArithmeticSlices(arr));
    }
    public static int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length ;
        // k ,   [i] , [j]   ==> k 可能有多个, 所以用list来存
        // 记录 k 的值 , k 对应的各个下标
        Map <Long, List<Integer>> hash =new HashMap<>();
        int sum = 0 ;
        // 状态表示: dp[i][j]  以 i , j 结尾的等差序列个数
        int[][] dp = new int[n][n];
        for(int i=0 ; i < n ; i ++){
            for(int j=i+1 ; j < n ; j ++){
                long d = (long) nums[j] - (long)nums[i] ;
                long k = (long) nums[i] - d ;
                if(hash.containsKey(k)){
                    List<Integer> ret = hash.get(k) ;
                    for(int z : ret){
                        dp[i][j] += dp[z][i] + 1 ;
                    }
                }
                sum += dp[i][j] ;
            }
            if(hash.containsKey((long) nums[i])){
                hash.get((long)nums[i]).add(i) ;
            }else{
                hash.put((long)nums[i] , new ArrayList<>());
                hash.get((long)nums[i]).add(i) ;
            }
        }
        return  sum ;
    }
}